package com.aoip.telemetry.telemetry.configureui.business.tool.analysis.controller;

import com.aoip.telemetry.telemetry.configureui.common.core.controller.BaseController;
import com.aoip.telemetry.telemetry.core.entity.AjaxResult;
import com.aoip.telemetry.telemetry.entity.common.*;
import com.aoip.telemetry.telemetry.entity.element.ElementConfigEntity;
import com.aoip.telemetry.telemetry.entity.element.ElementMessageEntity;
import com.aoip.telemetry.telemetry.entity.sn.SnHydrologyEntity;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 原始报文解析工具类
 *
 * @author chenbai
 * @version 1.0
 * @date 2022/1/25 9:38
 */
@RestController
public class MsgAnalysisToolController extends BaseController {



    /**
     * @param msg 原始报文
     * @return void
     * @author chenbai
     * @date 2022/1/25
     **/
    @PostMapping("/v1/GET/msg/analysis/tool")
    public AjaxResult analysis(@Validated @RequestBody String msg) {
        List<MsgAnalysisItemEntity> resultList = new ArrayList<>();
        if (!StringUtils.hasText(msg)) {
            return AjaxResult.success();
        }
        try {
            if (msg.startsWith("ST")) {
                resultList = snHydrologyAnalysis(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("解析失败！请核对报文信息。" + e.getMessage());
        }
        return AjaxResult.success(resultList);
    }




    /**
     * 水资源报文解析工具
     *
     * @param msg 原始报文
     * @return java.util.List<com.aoip.telemetry.telemetry.entity.common.MsgAnalysisItemEntity>
     * @author chenbai
     * @date 2022/1/26
     **/
    private List<MsgAnalysisItemEntity> snHydrologyAnalysis(String msg) {
        int length = msg.length();
        String frameStartCharacter = msg.substring(0, 2);
        String stCode = msg.substring(3, 13);
        String elementData = msg.substring(14, length - 4);
        String crc = msg.substring(length - 4, length - 2);
        String messageEndCharacter = msg.substring(length - 2);

        OriginalMessageEntity originalMsg = new OriginalMessageEntity();
        originalMsg.setMsgBody(elementData);
        originalMsg.setMsgText(msg);
        originalMsg.setStCode(stCode);

        SnHydrologyEntity snHydrologyEntity = new SnHydrologyEntity();
        snHydrologyEntity.setAllMsg(msg);
        snHydrologyEntity.setFrameStartCharacter(frameStartCharacter);
        snHydrologyEntity.setStCode(stCode);
        snHydrologyEntity.setElementData(elementData);
        snHydrologyEntity.setCrc(crc);
        snHydrologyEntity.setMessageEndCharacter(messageEndCharacter);
        Map<String, Object> elementDataDecode = new HashMap<>();
//        Map<String, Object> elementDataDecode = snHydrologyMsgDecode.elementDataDecode(elementData, originalMsg, snHydrologyEntity);

        List<MsgAnalysisItemEntity> resultList = new ArrayList<>();
        if (elementDataDecode != null) {
            resultList.add(new MsgAnalysisItemEntity("帧起始符", snHydrologyEntity.getFrameStartCharacter()));
            resultList.add(new MsgAnalysisItemEntity("遥测站地址", snHydrologyEntity.getStCode()));

            List<ElementMessageEntity> elementList = (List<ElementMessageEntity>) elementDataDecode.get("elementList");
            elementList.forEach(item -> {
                ElementConfigEntity entity = item.getElementConfigEntity();
                StringBuilder sb = new StringBuilder(entity.getIdentifierMsg());
                String identifierValue = entity.getIdentifierValue();
                String fieldUnit = entity.getFieldUnit();
                if (StringUtils.hasText(identifierValue)) {
                    sb.append("(要素标识符：").append(identifierValue).append(")");
                }
                if (StringUtils.hasText(fieldUnit)) {
                    sb.append("(单位：").append(fieldUnit).append(")");
                }
                resultList.add(new MsgAnalysisItemEntity(sb.toString(), item.getValue()));
            });
            resultList.add(new MsgAnalysisItemEntity("校验码", snHydrologyEntity.getCrc()));
            resultList.add(new MsgAnalysisItemEntity("报文结束符", snHydrologyEntity.getMessageEndCharacter()));
        }
        return resultList;
    }
}
